package com.xinfu.newproject.activity;

import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.common.SDActivityManager;
import com.xinfu.newproject.ui.LoginFragment;
import com.xinfu.newproject.ui.login_register.BackHandledInterface;
import com.xinfu.newproject.ui.login_register.ChildLoginFragment;
import com.xinfu.newproject.ui.login_register.RegisterFragment;
import com.xinfu.newproject.util.BToast;

import butterknife.ButterKnife;

public class LoginActivity extends BaseActivity implements BackHandledInterface {

    private FragmentManager fragmentManager;
    public final static int FRAGMENT_ACTIVITY = 0;
    public final static int FRAGMENT_ONE = 1;
    public final static int FRAGMENT_TWO = 2;
    public final static int FRAGMENT_THREE = 3;

    private LoginFragment loginFragment;
    private ChildLoginFragment childLoginFragment;
    private RegisterFragment registerFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        fragmentManager = getSupportFragmentManager();
        showFragment(FRAGMENT_ONE, "");

    }

    public void showFragment(int index, String respose) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        hideFragments(ft);
        Bundle bundle = new Bundle();
        bundle.putString("respose", respose);
        //注意这里设置位置
//        position = index;
        switch (index) {
            case FRAGMENT_ACTIVITY:
                this.finish();
                break;
            case FRAGMENT_ONE:
                /**
                 * 如果Fragment为空，就新建一个实例
                 * 如果不为空，就将它从栈中显示出来
                 */
                if (loginFragment == null) {
                    loginFragment = new LoginFragment();

                    ft.add(R.id.activity_login_fl, loginFragment);

                } else {
                    ft.show(loginFragment);
                }
                break;
            case FRAGMENT_TWO:
                if (childLoginFragment == null) {
                    childLoginFragment = new ChildLoginFragment();
                    childLoginFragment.setArguments(bundle);
                    ft.add(R.id.activity_login_fl, childLoginFragment);
                } else {
                    childLoginFragment.setArguments(bundle);
                    ft.show(childLoginFragment);
                }
                break;
            case FRAGMENT_THREE:
                if (registerFragment == null) {
                    registerFragment = new RegisterFragment();
                    registerFragment.setArguments(bundle);
                    ft.add(R.id.activity_login_fl, registerFragment);
                } else {
                    registerFragment.setArguments(bundle);
                    ft.show(registerFragment);
                }
                break;
        }
        ft.commitAllowingStateLoss();
    }

    @Override
    public void setSelectedFragment(int fragment, String respose) {
        showFragment(fragment, respose);
    }


    /**
     * 隐藏fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (loginFragment != null) {
            transaction.hide(loginFragment);
        }
        if (childLoginFragment != null) {
            transaction.hide(childLoginFragment);
        }
        if (registerFragment != null) {
            transaction.hide(registerFragment);
        }
    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }

    //退出app
    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
        }

        return true;
    }

    public void exitApp() {
        if (System.currentTimeMillis() - mExitTime > 1200) {
            BToast.showText(this, "再按一次退出应用");

        } else {
            //销毁activity
            SDActivityManager.getInstance().finishAllActivity();
            System.exit(0);
        }
        mExitTime = System.currentTimeMillis();
    }
}
